void main() {
  throwKeyword();
  //onStatementBlock();
  // onCatchStatementBlock();
  // onCatchStatementBlock2();
  // catchStatementBlock();
  // finallyCatchStatementBlock();
}

void throwKeyword() {
  try {
    testAge(-2);
  } catch (error) {
    print(error);
  }
}

void testAge(int age) {
  if (age < 0) {
    // throw new FormatException("年龄不能小于 0");
    throw FormatException("年龄不能小于 0");
  }
}

void onStatementBlock() {
  int x = 12;
  int y = 0;
  int res;

  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print("除数不能为零");
  }
}

void catchStatementBlock() {
  int x = 12;
  int y = 0;
  int res;

  try {
    res = x ~/ y;
  } catch (error) {
    print(error);
  }
}

void onCatchStatementBlock() {
  int x = 12;
  int y = 0;
  int res;

  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException catch (error) {
    print(error);
  }

}
void onCatchStatementBlock2() {
  try {
    try {
      int num = 100 ~/ 0;
    } on IntegerDivisionByZeroException {
      rethrow;//重新抛出一个异常
    }
  } catch (e, stackTrace) {
//将捕获到内层重新抛出的异常
  print(e);
  }
}

void finallyCatchStatementBlock() {
  int x = 12;
  int y = 0;
  int res;

  try {
    res = x ~/ y;
  } on IntegerDivisionByZeroException {
    print("Cannot divide by zero");
  } finally {
    print("Finally block executed");
  }
}
